package cn.ffcs.bpm.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.model.BaseEntity;
import cn.ffcs.base.service.BaseService;
import cn.ffcs.bpm.dao.IProcessConfigDetailDAO;
import cn.ffcs.bpm.dao.IProcessConfigDAO;
import cn.ffcs.bpm.dao.ITransactProcessStatusDAO;
import cn.ffcs.bpm.model.BizProcessManage;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.businessrequest.service.BusinessRequestService;

public class BizProcessManageService<B extends BaseEntity> extends BaseService<B>
implements IBizProcessManageService<B>
{

	private BizProcessManage bizProcessManage;
    /** 流程办理状态服务 **/
    private ITransactProcessStatusService transactProcessStatusService;
    /** 办理人服务 **/
    private ITransactorService transactorService;
    /** 办理人类型服务 **/
    private IProcTranRecordService procTranRecordService;
    /** 业务需求基本信息服务 **/
    private BusinessRequestService businessRequestService;
    /** 业务需求基本信息服务 **/
    private ITransactorTypeService transactorTypeService;
    
    /**
     * @return the transactorTypeService
     */
    public ITransactorTypeService getTransactorTypeService()
    {
        return transactorTypeService;
    }

    /**
     * @param transactorTypeService the transactorTypeService to set
     */
    public void setTransactorTypeService(
            ITransactorTypeService transactorTypeService)
    {
        this.transactorTypeService = transactorTypeService;
    }

    /** 流程办理状态 **/
    private ITransactProcessStatusDAO<TransactProcessStatus> tranactProcessStatusDAO;
    /** 流程配置 **/
    private IProcessConfigDAO<ProcessConfig> processConfigDAO;
    /** 流程节点配置 **/
    private IProcessConfigDetailDAO<ProcessConfigDetail> processConfigDetailDAO;
    
    /**
     * 初始化流程信息
     */
    public void initProcess(BaseEntity entity,String entityID)
    {
        boolean isExistProcessStatus=false;
        //查询是否存在流程办理状态信息
        Criterion cond1 = Restrictions.eq("refEntity", entity
                .getClass().getName());
        Criterion cond2 = Restrictions.eq("refEntityId", entityID);
        Criterion cond = Restrictions.and(cond1, cond2);
        List<TransactProcessStatus> lTps=tranactProcessStatusDAO.findBy(cond);
        TransactProcessStatus currProcessStatus=null;
        //获得流程步骤信息，
        if (lTps!=null && lTps.size()>0){
            isExistProcessStatus=true;
            currProcessStatus=lTps.get(0);
        }
        // 当前业务数据没有当前流程数据，取开始节点
        if (!isExistProcessStatus){
            //先获取默认流程
            Criterion cpc1 = Restrictions.eq("refEntity", entity
                    .getClass().getName());
            Criterion cpc2 = Restrictions.eq("isDefault", "1");
            Criterion cpc = Restrictions.and(cpc1, cpc2);
            List<ProcessConfig> lpc=processConfigDAO.findBy(cpc);
            
            if (lpc!=null && lpc.size()>0){
                //获取默认流程配置
                Criterion cpcd1 = Restrictions.eq("refEntity", entity
                        .getClass().getName());
                Criterion cpcd2 = Restrictions.eq("nodeType", "0");
                Criterion cpcd3 = Restrictions.eq("processConfig", lpc.get(0));
                Criterion cpcd4 = Restrictions.and(cpcd1, cpcd2);
                Criterion cpcd = Restrictions.and(cpcd3, cpcd4);
                List<ProcessConfigDetail> lpcd=processConfigDetailDAO.findBy(cpcd);
                if (lpcd!=null && lpcd.size()>0){
                    ProcessConfigDetail currProcessConfig=lpcd.get(0);
                    currProcessStatus=new TransactProcessStatus();
                    currProcessStatus.setCurrProcessConfigDetail(currProcessConfig);
                }
            }
        }

        // 当前业务数据有当前流程数据，取当前流程数据
        if (isExistProcessStatus){
            
        }
    }
    
//    /**
//     * 新增或修改操作
//     * @param baseEntity
//     */
//    public void saveOrUpdate(BizProcessManage bizProcessManage)
//    {
//        
//        businessRequestService.saveOrUpdate(bizProcessManage.getBusinessRequest());
//        transactProcessStatusService.saveOrUpdate(bizProcessManage.getTransactProcessStatus());
//        transactorService.saveOrUpdate(bizProcessManage.getCurTransactor());
//        procTranRecordService.saveOrUpdate(bizProcessManage.getProcTranRecord());
//    }
    
    
    
    /**
     * 流程提交操作
     */
    public void submit(){
        
    }
    
    /**
     * @return the processConfigDAO
     */
    public IProcessConfigDAO<ProcessConfig> getProcessConfigDAO()
    {
        return processConfigDAO;
    }

    /**
     * @param processConfigDAO the processConfigDAO to set
     */
    public void setProcessConfigDAO(
            IProcessConfigDAO<ProcessConfig> processConfigDAO)
    {
        this.processConfigDAO = processConfigDAO;
    }

    /**
     * @return the tranactProcessStatusDAO
     */
    public ITransactProcessStatusDAO<TransactProcessStatus> getTranactProcessStatusDAO()
    {
        return tranactProcessStatusDAO;
    }

    /**
     * @param tranactProcessStatusDAO the tranactProcessStatusDAO to set
     */
    public void setTranactProcessStatusDAO(
            ITransactProcessStatusDAO<TransactProcessStatus> tranactProcessStatusDAO)
    {
        this.tranactProcessStatusDAO = tranactProcessStatusDAO;
    }

    /**
     * @return the processConfigDetailDAO
     */
    public IProcessConfigDetailDAO<ProcessConfigDetail> getProcessConfigDetailDAO()
    {
        return processConfigDetailDAO;
    }

    /**
     * @param processConfigDetailDAO the processConfigDetailDAO to set
     */
    public void setProcessConfigDetailDAO(
            IProcessConfigDetailDAO<ProcessConfigDetail> processConfigDetailDAO)
    {
        this.processConfigDetailDAO = processConfigDetailDAO;
    }

    /**
     * @return the transactProcessStatusService
     */
    public ITransactProcessStatusService getTransactProcessStatusService()
    {
        return transactProcessStatusService;
    }

    /**
     * @param transactProcessStatusService the transactProcessStatusService to set
     */
    public void setTransactProcessStatusService(
            ITransactProcessStatusService transactProcessStatusService)
    {
        this.transactProcessStatusService = transactProcessStatusService;
    }

    /**
     * @return the bizProcessManage
     */
    public BizProcessManage getBizProcessManage()
    {
        return bizProcessManage;
    }

    /**
     * @param bizProcessManage the bizProcessManage to set
     */
    public void setBizProcessManage(BizProcessManage bizProcessManage)
    {
        this.bizProcessManage = bizProcessManage;
    }

    /**
     * @return the transactorService
     */
    public ITransactorService getTransactorService()
    {
        return transactorService;
    }

    /**
     * @param transactorService the transactorService to set
     */
    public void setTransactorService(ITransactorService transactorService)
    {
        this.transactorService = transactorService;
    }

    /**
     * @return the procTranRecordService
     */
    public IProcTranRecordService getProcTranRecordService()
    {
        return procTranRecordService;
    }

    /**
     * @param procTranRecordService the procTranRecordService to set
     */
    public void setProcTranRecordService(
            IProcTranRecordService procTranRecordService)
    {
        this.procTranRecordService = procTranRecordService;
    }

    /**
     * @return the businessRequestService
     */
    public BusinessRequestService getBusinessRequestService()
    {
        return businessRequestService;
    }

    /**
     * @param businessRequestService the businessRequestService to set
     */
    public void setBusinessRequestService(
            BusinessRequestService businessRequestService)
    {
        this.businessRequestService = businessRequestService;
    }
}
